home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
9-Digit Zip Code Directory
/
9-Digit Zip Code Directory (American Business Information) (ABIZIP-12).ISO
/
z4src.zip
/
BSWRAP.C
< prev
next >
Wrap
C/C++ Source or Header
|
1993-09-02
|
5KB
|
176 lines
//----------------------------------------------------------------------------
// MODULE DESCRIPTION
//
// Module: bswrap.c
// Title: Base library
// Notice: John M. Weeder
// Copyright (c) 1993. All rights reserved.
// This module contains proprietary information and should be
// treated as confidential.
//
//----------------------------------------------------------------------------
// MAINTENANCE HISTORY
//
// $Workfile$
// $Revision$
// $Author$
// $Date$
// $Log$
//
//----------------------------------------------------------------------------
// MODULE NARRATIVE
//
//
// This module contains .
//
// The code in this module should be written entirely in C.
// Do not use any C++ constructs.
//
// This module is portable to:
// DOS 3.X+
// MS Windows 3.X+
// MS Windows NT
// OS/2 2.X+
// OS/2 2.0 PM
// SCO UNIX.
//
// The following compilers are supported:
// MSC 6.0A
// MSC/C++ 7.0
// Borland C++ 3.1 for DOS
// Borland C++ 1.0 for OS/2 2.X
// SCO UNIX cc
//
//----------------------------------------------------------------------------
#include <bs.h>
//----------------------------------------------------------------------------
// Description: Determine where to break a string in order to word wrap.
// Hard linefeeds are handled. A linefeed will always be
// found in the line being displayed.
// Parameters: ppcsz Pointer to buffer which contains text to wrap.
// This pointer is adjust to reflect the first
// character of the next line.
// cSize Size of line (Does not include \n) to wrap to.
// pszOut Output buffer
// This buffer must be large enough to contain at
// least one line of text:
// SIZE = cSize + (fLf ? 1: 0) + 1
// If null, no text is copied to the buffer
// fLf Append linefeed.
// Globals:
// Returns: Pointer to first character in output buffer, or NULL.
//----------------------------------------------------------------------------
BOOL FN_E strwrap(PCSZ _FAR_ *ppcsz, SIZET cSize, PSZ pszOut, BOOL fLf)
{
PCSZ pcsz = *ppcsz;
SIZET cLen, i;
PCSZ pcszLf;
Assert(ppcsz);
Assert(cSize);
cLen = strlen(pcsz);
if (!cLen)
return FALSE;
pcszLf = strchr(pcsz, '\n');
if (pcszLf != NULL)
{ // Found an explicit carriage return
SIZET cBuf = (SIZET)(pcszLf - pcsz);
if (cBuf <= cSize) // Check if all text up to and including
{ // the carriage return will fit
if (pszOut)
{
memcpy(pszOut, pcsz, cBuf);
pszOut[cBuf] = '\0'; // Terminate buffer
}
pcsz = pcszLf + 1; // Store pointer to next character
goto ERROR_EXIT;
}
}
if (cLen <= cSize) // Remaining text will fit on one line
{
if (pszOut)
strcpy(pszOut, pcsz);
pcsz += cLen; // Move to next line (null)
goto ERROR_EXIT;
}
// Try to break at space
for (i = cSize; i + 1; --i)
if (isspace(pcsz[i]))
{
if (pszOut) // Copy text
{
if (i)
memcpy(pszOut, pcsz, i);
pszOut[i] = '\0';
}
pcsz += i + 1;
goto ERROR_EXIT;
}
if (pszOut)
{
memcpy(pszOut, pcsz, cSize); // Couldn't find a place to break
pszOut[cSize] = '\0'; // Grap a bunch of text, ignore spaces
}
pcsz += cSize;
ERROR_EXIT:
*ppcsz = pcsz; // Update pointer to next char
if (fLf && pszOut) // Append linefeed
strcat(pszOut, "\n");
return TRUE;
}
//----------------------------------------------------------------------------
// Description: Determine the number of lines of text in a buffer which
// has been word wrapped.
// Parameters: cSize Width to wrap to.
// Globals:
// Returns: Number of lines of text.
//----------------------------------------------------------------------------
SIZET FN_E strwrapcount(PCSZ pcsz, SIZET cSize)
{
SIZET cLines = 0;
while (strwrap(&pcsz, cSize, NULL, FALSE))
cLines++;
return cLines;
}
//----------------------------------------------------------------------------
// Description: Run standard test suite
// Parameters: sTest Test to run.
// 0 Run all default tests (except).
// Returns: TRUE if successful.
//----------------------------------------------------------------------------
#if COMPILE_TEST
BOOL FN WrapTest(SHORT sTest)
{
static PCSZ pcszWrap = "12345\n\n123 456\n12345 123456\n123\n1";
CHAR szBuf[20];
PCSZ pcsz = pcszWrap;
NOTUSED(sTest);
Output("Number of lines = %d\n", strwrapcount(pcsz, 5));
while (strwrap(&pcsz, 5, szBuf, FALSE))
Output("[%s]\n", szBuf);
return TRUE;
}
#endif
//----------------------------------------------------------------------------
//------------------------------- End of File --------------------------------
//----------------------------------------------------------------------------